package com.omega.controller;

import java.util.Iterator;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.omega.common.utils.Utils;
import com.omega.constants.ApplicationConstants;
import com.omega.entity.Retailer;
import com.omega.service.RetailerService;
import com.omega.vo.EntityListVO;
import com.omega.vo.RetailersListVO;

@Controller
@RequestMapping(value = "/admin/retailer")
public class RetailerController {
	
	@Autowired
	RetailerService retailerService;
	
	public RetailerController() {
	}

	@RequestMapping(value="/addRetailer", method=RequestMethod.GET)
	public String showAddRetailerForm(Model model){
		RetailersListVO retailersListVO = EntityListVO.getRetailersListVO();
		model.addAttribute(ApplicationConstants.RETAILERS_LIST_VO, retailersListVO);
		model.addAttribute("yearMap", Utils.getYearsMap());
		model.addAttribute("monthMap", Utils.getMonthsMap());
		return ApplicationConstants.ADD_NEW_RETAILER_VIEW;
	}
	
	@RequestMapping(value="/addRetailer", method=RequestMethod.POST)
	public String submitAddRetailerForm(@ModelAttribute(ApplicationConstants.RETAILERS_LIST_VO) RetailersListVO retailersListVO, Model model){
		for (Iterator<Retailer> iterator = retailersListVO.getRetailersList().iterator(); iterator.hasNext();) {
			Retailer retailer = (Retailer) iterator.next();
			if(! StringUtils.isEmpty(retailer.getTinNumber()))
				retailerService.addRetailer(retailer);	
		}
		return ApplicationConstants.REDIRECT_RETAILERS_VIEW;
	}
	
	@RequestMapping(value = "/viewRetailers", method = RequestMethod.GET)
	public String viewAllSuppliers(Model model) {
		model.addAttribute(ApplicationConstants.RETAILERS, retailerService.loadAllRetailers());
		return ApplicationConstants.RETAILERS_VIEW;
	}
	
	@RequestMapping(value = "/editRetailer/{retailerId}", method = RequestMethod.GET)
	public String showEditRetailerForm(@PathVariable String retailerId, Model model) {
		
		Retailer retailer = retailerService.loadById(Integer.parseInt(retailerId));
		model.addAttribute("yearMap", Utils.getYearsMap());
		model.addAttribute("monthMap", Utils.getMonthsMap());
		model.addAttribute(ApplicationConstants.RETAILER_OBJ, retailer);
		return ApplicationConstants.EDIT_RETAILER_VIEW;
	}
	
	@RequestMapping(value = "/editRetailer/{retailerId}", method = RequestMethod.POST)
	public String submitEditSupplierForm(@ModelAttribute(ApplicationConstants.RETAILER_OBJ) Retailer retailer) {
		retailerService.updateRetailer(retailer);
		return ApplicationConstants.REDIRECT_RETAILERS_VIEW;
	}
	
	@RequestMapping(value = "/activateRetailer/{retailerId}", method = RequestMethod.GET)
	public String activateRetailer(@PathVariable("retailerId") String retailerId) {
		retailerService.activateRetailer(Integer.parseInt(retailerId));
		return ApplicationConstants.REDIRECT_RETAILERS_VIEW;
	}
	
	@RequestMapping(value = "/deactivateRetailer/{retailerId}", method = RequestMethod.GET)
	public String deactivateRetailer(@PathVariable("retailerId") String retailerId) {
		retailerService.deactivateRetailer(Integer.parseInt(retailerId));
		return ApplicationConstants.REDIRECT_RETAILERS_VIEW;
	}
}
